/*
    This file is part of Emu.

    Emu is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Emu is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Emu.  If not, see <http://www.gnu.org/licenses/>. */
#include <QApplication>
#include <QKeyEvent>

#include "etextedit.h"

ETextEdit::ETextEdit()
    : _historyTemp(false), _historyCount(0), _historyIndex(0)
{
     /* The three variables above do management of session history, which is pretty tricky.
        It's shell-like, in that no duplicates are allowed, a temporary is stored if you
         move back, and modifying + sending an entry results in the modified copy being
         appended.
        Remember : Index and Count are ahead, so .at(Count - 1) contains the current.
                   The temp, if it exists, is at .at(Count). */
}

void ETextEdit::historyAdd()
{
    /* Data was just sent, so add it to history. The window still contains the text before
        aliases ran. */
    QString text = toPlainText();

    if (_historyTemp) {
        /* Ensure that the item to be added is not a duplicate of the previously added
            item. */
        _historyTemp = false;
        if (_historyData.at(_historyCount - 1) == text) {
            /* Remove the temp, and set Index to Count. This will stop historyAdd from
                moving up, and historyDown will see Index == Count and add a temp. */

            _historyData.removeAt(_historyCount);
            _historyIndex = _historyCount;
            return;
        }
        else {
            /* The stash is not a duplicate. Assign to it, then fall below so Index and
                Count increment. That will make the temp into a permanent entry. */
            _historyData[_historyCount] = text;
        }
    }
    else {
        if (_historyCount) {
            /* Make sure this one isn't a duplicate of the last. */
            if (_historyData.at(_historyCount - 1) != text) {
                /* No stash, just toss it in. */
                _historyData.append(text);
            }
            else {
                /* It's a dup, do nothing. */
                return;
            }
        }
        else {
            /* No previous entries, this can't be a duplicate. Add. */
            _historyData.append(text);
        }
    }

    _historyCount++;
    _historyIndex = _historyCount;
}

void ETextEdit::historyDown()
{
    /* Can't go down from the bottom! */
    if (_historyIndex == 0)
        return;

    if (_historyIndex == _historyCount) {
        /* The user is working on their next entry, and they want to go backward.
           Store the next entry in a stash so it can be fetched later. */
        if (!_historyTemp) {
            /* The stash doesn't exist yet, make it. */
            _historyData.append(toPlainText());
            _historyTemp = true;
        }
        else {
            /* Only modify the temp if it's necessary. */
            if (document()->isModified())
                _historyData[_historyIndex] = toPlainText();
        }
    }
    else {
        /* Update the current entry if it needs to be updated. */
        if (document()->isModified())
            _historyData[_historyIndex] = toPlainText();
    }

    _historyIndex--;
    setPlainText(_historyData.at(_historyIndex));
}

void ETextEdit::historyUp()
{
    /* Can't go forward from the last entry. */
    if (_historyIndex == _historyCount)
        return;

    /* If a modification occured, update whatever this entry contained. */
    if (document()->isModified())
        _historyData[_historyIndex] = toPlainText();

    _historyIndex++;
    setPlainText(_historyData.at(_historyIndex));
}

